﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class ProfitAndLossDistributionController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /ProfitAndLossDistribution/

        public ActionResult Index(int uid, string pdate)
        {
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetSettlementModel(pdate, db);//查询数据库
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("querydate", pdate, 20);
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (th != null && th.YPdate != 0)
                {
                    int tid = th.Id;
                    int operatorId = uid;
                    BenefitHelper.Sys.Operator op = new BenefitHelper.Sys.Operator().GetModel(operatorId, db);
                    ViewBag.op = op;

                    ViewBag.pdate = pdate;
                    string stringPdate = new BenefitHelper.Sys.TradeHistory().AddHorizontalLineIntoPdate(pdate);
                    ViewBag.Hpdate = stringPdate;
                    bool traded = op.OperatorTradedAtDay(operatorId, tid, db);
                    if (traded)
                    {

                        BenefitHelper.View.OperatorMonthSum osum = new BenefitHelper.View.OperatorMonthSum();
                        osum = osum.GetOperatorMonthSum(uid, th.MPdate, th.YPdate, db);//月累计
                        ViewBag.osum = osum;
                        BenefitHelper.View.OperatorDayChange ochange = new BenefitHelper.View.OperatorDayChange();
                        ochange = ochange.GetOperatorDayChange(uid, tid, db);//当日交易信息
                        ViewBag.ochange = ochange;

                        BenefitHelper.View.OperatorDayTradeInfo info = new BenefitHelper.View.OperatorDayTradeInfo();
                        string js = "";

                        #region 分时情况 盈利
                        BenefitHelper.View.OperatorDayTradeInfo info3 = info.GetOperatorDayTradeInfo(th.Id, operatorId, "sumwin", db);
                        js += BenefitHelper.Tools.ChartAtManager.BenefitTimePie(info3, "showflash2", "盈利示意图", "BenefitTimePie2");//ok
                        ViewBag.WinTimePie = info3;
                        BenefitHelper.View.OperatorDayTradeInfoData WinSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        WinSumData.BillCount = info3.Datas.Sum(a => a.BillCount);
                        WinSumData.SumValue = info3.Datas.Sum(a => a.SumValue);
                        ViewBag.WinSumData = WinSumData;
                        #endregion

                        #region 分时情况 亏损
                        BenefitHelper.View.OperatorDayTradeInfo info2 = info.GetOperatorDayTradeInfo(th.Id, operatorId, "sumlost", db);
                        js += BenefitHelper.Tools.ChartAtManager.BenefitTimePie(info2, "showflash", "亏损示意图", "BenefitTimePie");//ok
                        ViewBag.LostTimePie = info2;
                        BenefitHelper.View.OperatorDayTradeInfoData LostSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        LostSumData.BillCount = info2.Datas.Sum(a => a.BillCount);
                        LostSumData.SumValue = info2.Datas.Sum(a => a.SumValue);
                        ViewBag.LostSumData = LostSumData;
                        js += BenefitHelper.Tools.ChartAtManager.WinAndLose(ochange);//ok
                        ViewBag.js = js;
                        #endregion

                        #region 品种交易情况 盈利
                        List<BenefitHelper.View.OperatorDayProductInfo> infos = new BenefitHelper.View.OperatorDayProductInfo().GetOperatorDayProductInfo(th.Id, operatorId, true, db);
                        ViewBag.WinProductInfos = infos;
                        //   js += BenefitHelper.Tools.ChartAtManager.BenefitProductPie(infos, "headdiv2", "盈利示意图", "BenefitProductPie2");
                        BenefitHelper.View.OperatorDayTradeInfoData WinWinSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        WinWinSumData.BillCount = infos.Sum(a => a.BillCount);
                        WinWinSumData.SumValue = infos.Sum(a => a.Profit);
                        ViewBag.WinWinSumData = WinWinSumData;
                        #endregion

                        #region 品种交易情况 亏损
                        List<BenefitHelper.View.OperatorDayProductInfo> infos2 = new BenefitHelper.View.OperatorDayProductInfo().GetOperatorDayProductInfo(th.Id, operatorId, false, db);
                        ViewBag.LostWinInfos = infos2;
                        //     js += BenefitHelper.Tools.ChartAtManager.BenefitProductPie(infos2, "headdiv", "亏损示意图", "BenefitProductPie");
                        BenefitHelper.View.OperatorDayTradeInfoData LostWinSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        LostWinSumData.BillCount = infos2.Sum(a => a.BillCount);
                        LostWinSumData.SumValue = infos2.Sum(a => a.Profit);
                        ViewBag.LostWinSumData = LostWinSumData;
                        #endregion

                        #region 多空方向 盈利
                        BenefitHelper.View.OperatorDayDirectionInfo info4 = new BenefitHelper.View.OperatorDayDirectionInfo();
                        info4 = info4.GetOperatorDayDirectionInfo(th.Id, operatorId, true, db);
                        ViewBag.WinDirection = info4;
                        //   js += BenefitHelper.Tools.ChartAtManager.BenefitHeadPie(info4, "headdiv2", "盈利示意图", "BenefitProductPie2");
                        BenefitHelper.View.OperatorDayTradeInfoData WinDirectionSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        WinDirectionSumData.BillCount = info4.Datas.Sum(a => a.BillCount);
                        WinDirectionSumData.SumValue = info4.Datas.Sum(a => a.SumValue);
                        ViewBag.WinDirectionSumData = WinDirectionSumData;
                        #endregion

                        #region 多空方向 亏损
                        BenefitHelper.View.OperatorDayDirectionInfo info5 = new BenefitHelper.View.OperatorDayDirectionInfo();
                        info5 = info5.GetOperatorDayDirectionInfo(th.Id, operatorId, false, db);
                        ViewBag.LostDirection = info5;
                        //   js += BenefitHelper.Tools.ChartAtManager.BenefitHeadPie(info5, "headdiv", "亏损示意图", "BenefitProductPie");
                        BenefitHelper.View.OperatorDayTradeInfoData LostDirectionSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        LostDirectionSumData.BillCount = info5.Datas.Sum(a => a.BillCount);
                        LostDirectionSumData.SumValue = info5.Datas.Sum(a => a.SumValue);
                        ViewBag.LostDirectionSumData = LostDirectionSumData;
                        #endregion

                        #region 按收益情况 盈利
                        BenefitHelper.View.OperatorDayWinNum info6 = new BenefitHelper.View.OperatorDayWinNum();
                        info6 = info6.GetOperatorDayWinNum(th.Id, operatorId, true, db);
                        ViewBag.WinNum = info6;
                        //  js += BenefitHelper.Tools.ChartAtManager.BenefitMoneyPie(info6, "moneydiv2", "盈利示意图", "BenefitMoneyPie2");

                        BenefitHelper.View.OperatorDayTradeInfoData WinNumSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        WinNumSumData.BillCount = info6.Datas.Sum(a => a.BillCount);
                        WinNumSumData.SumValue = info6.Datas.Sum(a => a.SumValue);
                        ViewBag.WinNumSumData = WinNumSumData;
                        #endregion

                        #region 按收益情况 亏损
                        BenefitHelper.View.OperatorDayWinNum info7 = new BenefitHelper.View.OperatorDayWinNum();
                        info7 = info7.GetOperatorDayWinNum(th.Id, operatorId, false, db);
                        ViewBag.LostNum = info7;
                        //   js += BenefitHelper.Tools.ChartAtManager.BenefitMoneyPie(info7, "moneydiv", "亏损示意图", "BenefitProductPie");
                        BenefitHelper.View.OperatorDayTradeInfoData LostNumSumData = new BenefitHelper.View.OperatorDayTradeInfoData();
                        LostNumSumData.BillCount = info7.Datas.Sum(a => a.BillCount);
                        LostNumSumData.SumValue = info7.Datas.Sum(a => a.SumValue);
                        ViewBag.LostNumSumData = LostNumSumData;
                        #endregion

                        db.Dispose();
                        return View();
                    }
                    else
                    {
                        db.Dispose();
                        return Redirect("/Error/index/" + uid + "/" + pdate);
                    }
                }
                else
                {
                    db.Dispose();
                    return Redirect("/Error/index/" + uid + "/" + pdate);
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
        }

    }
}
